home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / s_test.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  119 lines

  1. ;$Id: s_test.pro,v 1.6 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       S_TEST
  8. ;
  9. ; PURPOSE:
  10. ;       This function tests the hypothesis that two sample popultions, 
  11. ;       {X[i], Y[i]}, have the same mean of distribution against the 
  12. ;       hypothesis that they differ. The result is a two-element vector
  13. ;       containing the maximum number of signed differences between 
  14. ;       corresponding pairs of X[i] and Y[i] and the one-tailed level of
  15. ;       significance. This type of test is often refered to as the Sign
  16. ;       Test.
  17. ;       
  18. ; CATEGORY:
  19. ;       Statistics.
  20. ;
  21. ; CALLING SEQUENCE:
  22. ;       Result = S_test(X, Y)
  23. ;
  24. ; INPUTS:
  25. ;       X:    An n-element vector of type integer, float or double.
  26. ;
  27. ;       Y:    An n-element vector of type integer, float or double.
  28. ;
  29. ; KEYWORD PARAMETERS:
  30. ;   ZDIFF:    Use this keyword to specify a named variable which returns the
  31. ;             number of differences between corresponding pairs of X[i] and 
  32. ;             Y[i] resulting in zero. Paired data resulting in a difference 
  33. ;             of zero are excluded from the ranking and the sample size is 
  34. ;             correspondingly reduced.
  35. ;
  36. ; EXAMPLE:
  37. ;       Define the n-element vectors of sample data.
  38. ;         x = [47, 56, 54, 49, 36, 48, 51, 38, 61, 49, 56, 52]
  39. ;         y = [71, 63, 45, 64, 50, 55, 42, 46, 53, 57, 75, 60]
  40. ;       Test the hypothesis that two sample popultions, {X[i], Y[i]}, have 
  41. ;       the same mean of distribution against the hypothesis that they differ
  42. ;       at the 0.05 significance level.
  43. ;         result = s_test(x, y, zdiff = zdiff)
  44. ;       The result should be the 2-element vector:
  45. ;         [9.00000, 0.0729981]
  46. ;       The keyword parameter should be returned as:
  47. ;         zdiff = 0
  48. ;       The computed probability (0.0729981) is greater than the 0.05 
  49. ;       significance level and therefore we do not reject the hypothesis that
  50. ;       X and Y have the same mean of distribution.
  51. ;
  52. ; PROCEDURE:
  53. ;       S_TEST computes the nonparametric Sign Test. Differences between 
  54. ;       corresponding pairs of X[i] and Y[i] are ranked as either positive or
  55. ;       negative with equal probability of occurance. Differences between 
  56. ;       pairs of X[i] and Y[i] that result in zero are excluded from the 
  57. ;       ranking and the sample size is correspondingly reduced. The result is 
  58. ;       a two-element vector [diff, p] containing the maximum number of signed 
  59. ;       differences between corresponding pairs of X[i] and Y[i] and the one-
  60. ;       tailed level of significance. Using the Binomial random variable X, 
  61. ;       we can accept of reject the proposed hypothesis. If the sample size 
  62. ;       exceeds 25, then the Gaussian distribution is used to approximate the 
  63. ;       cumulative Binomial distribution. The one-tailed probability of
  64. ;       obtaining at least (diff) signed differences in an n-element sample is
  65. ;       equal to (p). Prob(X >= diff) = p. 
  66. ;       The hypothesis that two sample popultions have the same mean
  67. ;       of distribution is rejected if the number of positive ranks and the
  68. ;       number of negative ranks differ with statistical significance. 
  69. ;
  70. ; REFERENCE:
  71. ;       PROBABILITY and STATISTICS for ENGINEERS and SCIENTISTS (3rd edition)
  72. ;       Ronald E. Walpole & Raymond H. Myers
  73. ;       ISBN 0-02-424170-9
  74. ;
  75. ; MODIFICATION HISTORY:
  76. ;       Written by:  GGS, RSI, August 1994
  77. ;-
  78.  
  79. function s_test, x, y, zdiff = zdiff
  80.  
  81.   on_error, 2
  82.   n = n_elements(x)
  83.   if n ne n_elements(y) then message, $
  84.       'x and y must be vectors of equal size.'
  85.  
  86.   diff = x - y
  87.  
  88.   ;Number of "ties" (identical data).
  89.   psize = where(diff eq 0, zdiff)
  90.  
  91.   ;Population sample size. 
  92.   psize = n - zdiff 
  93.  
  94.   if psize eq 0 then message, $
  95.     'x and y contain identical data.'
  96.  
  97.   ;Number of positive ranks.
  98.   ipn = where(diff gt 0, npos)
  99.  
  100.   ;Number of negative ranks.
  101.   nneg = psize - npos
  102.  
  103.   if npos gt nneg then begin
  104.   ;Probability that the number of positive ranks is at least (npos) with a
  105.   ;population size (psize).     Prob(# of positive ranks >= npos)
  106.     prob = binomial(npos, psize, 0.5)
  107.   endif else if nneg gt npos then begin 
  108.   ;Prob(# of negative ranks >= nneg)
  109.     prob = binomial(nneg, psize, 0.5)
  110.   endif else $
  111.     ;prob = binomial(npos, psize/2, 0.5)
  112.     prob = 0.5
  113.  
  114.   ;Maximum number of signed differences and the one-tailed probability.
  115.   return, [max([npos, nneg]), prob]
  116.  
  117. end
  118.  
  119.